package com.htgd.gardenexpo.hprose.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.hprose.HproseService;
import com.htgd.gardenexpo.utils.DecryptionUtils;
import com.htgd.gardenexpo.utils.HttpRequest;
import com.htgd.gardenexpo.utils.WXCore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @author ：hejing
 * @date ：Created in 2021/6/15 11:22
 * @description：hprose
 * @modified By：`
 * @version: 1.0
 */
@Service
@Component
public class HproseServiceImpl implements HproseService {
    public static final String authorization_code="authorization_code";

    @Value("${wx.code.url}")
    public String code_url;

    @Resource
    private RestTemplate restTemplate;

    @Override
    public String aesDecrypt(String appId, String encryptedData, String sessionKey, String iv) throws Exception {
        System.out.println("解密--"+new Date());
        String s= DecryptionUtils.decode(sessionKey,encryptedData,iv);
        System.out.println("解密--"+new Date());
        System.out.println("解密--"+s);
        return  s;
    }

    @Override
    public JSONObject decodeUserInfo(String appid, String js_code) {
        String params = "appid=" + appid + "&secret=fd9d3c4361f4a6f053519ff1c70e03af&js_code=" + js_code + "&grant_type="
                + authorization_code;
        System.out.println("调用weixin--"+new Date());
        ResponseEntity responseEntity = restTemplate.getForEntity
                (
                        code_url+"?"+params,
                        String.class
                );
        String re=responseEntity.getBody().toString();
//        String sr = HttpRequest.sendGet(code_url, params);
        System.out.println("调用weixin--"+new Date());
        try {
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(re);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
